package com.woniuxy.microfundservice.util;

import com.woniuxy.bean.BillBean;
import com.woniuxy.microfundservice.bean.AutomaticInvestment;
import com.woniuxy.microfundservice.bean.FundMsg;
import com.woniuxy.microfundservice.bean.info.FundFullInfo;
import com.woniuxy.microfundservice.dao.AppCodeDao;
import com.woniuxy.microfundservice.dao.AutomaticInvestmentDao;
import com.woniuxy.microfundservice.service.FundFullInfoService;
import com.woniuxy.microfundservice.service.MemberFundAccountService;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Auther: yeyang  <br/>
 * Date: 2021/4/15 0015:上午 11:29  <br/>
 * Description:
 */
@Component
public class DailyUpdate {
    @Resource
    public FundFullInfoService fundFullInfoService;

    @Resource
    private MemberFundAccountService memberFundAccountService;
    @Resource
    private AutomaticInvestmentDao automaticInvestmentDao;
    @Resource
    private AppCodeDao appCodeDao;

    @Resource
    private RestTemplate restTemplate;

    //每天更新基金信息和收益
    @Scheduled(cron="0 0 17 ? * *")
    public void myTask(){
        String appcode = appCodeDao.selectById("fund");
        HttpHeaders httpHeaders = new HttpHeaders();
        //定义传输的数据的数据方式
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.set(HttpHeaders.AUTHORIZATION, "APPCODE " + appcode);
        //组装数据
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, httpHeaders);
        //获取所有基金代码
        List<FundFullInfo> fundFullInfos = fundFullInfoService.selectAll();
        ResponseEntity<FundMsg> exchange =null;
        FundMsg fundMsg =null;
        FundFullInfo newInfo=new FundFullInfo();

        //遍历基金代码
        for (FundFullInfo fundFullInfo:fundFullInfos) {
            String code=String.format("%06d",fundFullInfo.getFundCode());
            String url = "https://fund.market.alicloudapi.com/getFundDetail?fundcode=" + code;
            exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, FundMsg.class);
            fundMsg = exchange.getBody();
            //获取新的fund对象
            newInfo=fundMsg.getFundFullInfo();
            newInfo.setId(fundFullInfo.getId());
            //批量更新基金详情
            fundFullInfoService.updateAll(newInfo);
            //批量更新个人基金收益相关信息
            memberFundAccountService.updateDaily(newInfo);
        }
    }

    @Scheduled(cron="0 0 14 ? * *")
    public void AutoInvist(){
        Calendar cal=Calendar.getInstance();
        //当天周几
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        //当天几号
        int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
        BillBean billBean=new BillBean();
        //每天定投
        List<AutomaticInvestment> everDay = automaticInvestmentDao.selectByType("每天", 0);
        //每周定投当天该投
        List<AutomaticInvestment> everWeek = automaticInvestmentDao.selectByType("每周", dayOfWeek);
        //每月定投该当天定投
        List<AutomaticInvestment> everMonth = automaticInvestmentDao.selectByType("每月", dayOfMonth);
        //合并当天该定投的数据
        everDay.addAll(everWeek);
        everDay.addAll(everMonth);
        //购买
        for (AutomaticInvestment ai:everDay) {
            billBean.setModifiedPrice(ai.getMoney());
            billBean.setMemberName(ai.getMemberName());
            billBean.setProductId(ai.getFundId());
            billBean.setPayPassword(ai.getBankPassword());
            billBean.setPayType(ai.getPayType());
            billBean.setBankCardId(ai.getBankCardId());
            memberFundAccountService.sureOrderDate(billBean);
        }
    }

}
